package xcmg.device.service.company;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.company.DealerLevelDO;
import xcmg.device.dao.mapper.company.DealerLevelMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.company.DealerLevelVO;
import yb.ecp.fast.feign.FastGenClient;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wrg on 2019/4/19.
 */
@Service
public class DealerLevelService {
    @Autowired
    private DealerLevelMapper dealerLevelMapper;
    @Autowired
    private FastGenClient genClient;
    /**
     * 获取客户分级列表
     * @return dealerLevelVOList 分级结果集
     */
    public List<DealerLevelVO> list(String orgId){
        List<DealerLevelVO> dealerLevelVOList = new ArrayList<>();
        List<DealerLevelDO> dealerLevelDOList = dealerLevelMapper.list(orgId);
        dealerLevelDOList.forEach(dealerLevelDO->{
            DealerLevelVO dealerLevelVO = new DealerLevelVO();
            BeanUtils.copyProperties(dealerLevelDO,dealerLevelVO);
            dealerLevelVOList.add(dealerLevelVO);
        });
        return dealerLevelVOList;
    }

    public ErrorCode insert(DealerLevelVO dealerLevelVO,String userId,String orgId){
        DealerLevelDO dealerClassDO = new DealerLevelDO();
        BeanUtils.copyProperties(dealerLevelVO,dealerClassDO);
        dealerClassDO.setCreateDate(new Date());
        dealerClassDO.setUpdateDate(new Date());
        dealerClassDO.setCreateId(userId);
        dealerClassDO.setUpdateId(userId);
        dealerClassDO.setOrgId(orgId);
        Integer nameCount=dealerLevelMapper.nameCount(dealerClassDO);
        if(nameCount>0){
            return ErrorCode.LevelNameDuplicated;
        }
        String id = genClient.newGuidText().getValue();
        if (StringUtils.isBlank(id)){
            return ErrorCode.PrimaryKeyGenerationFailed;
        }
        dealerClassDO.setId(id);
        return dealerLevelMapper.insertSelective(dealerClassDO)>0? ErrorCode.Success:ErrorCode.Failure;
    }

    public ErrorCode update(DealerLevelVO dealerLevelVO,String userId){
        Integer count = dealerLevelMapper.existCount(dealerLevelVO.getId());
        if(count <= 0)
        {
            return ErrorCode.IllegalArument;
        }

        DealerLevelDO dealerClassDO = new DealerLevelDO();
        dealerClassDO.setId(dealerLevelVO.getId());
        dealerClassDO.setLevelCode(dealerLevelVO.getLevelCode());
        dealerClassDO.setLevelName(dealerLevelVO.getLevelName());
        dealerClassDO.setUpdateId(userId);
        dealerClassDO.setUpdateDate(new Date());
        dealerClassDO.setOrgId(dealerLevelVO.getOrgId());
        Integer nameCount=dealerLevelMapper.nameCount(dealerClassDO);
        if(nameCount>0){
            return ErrorCode.LevelNameDuplicated;
        }
        return dealerLevelMapper.updateByPrimaryKeySelective(dealerClassDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public ErrorCode delete(String id,String userId)
    {
        Integer count = dealerLevelMapper.existCount(id);
        if(count <= 0)
        {
            return ErrorCode.IllegalArument;
        }
        DealerLevelDO dealerLevelDO = new DealerLevelDO();
        dealerLevelDO.setId(id);
        dealerLevelDO.setUpdateId(userId);
        dealerLevelDO.setUpdateDate(new Date());
        return dealerLevelMapper.deleteById(dealerLevelDO) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    public DealerLevelVO getDetail(String id)
    {
        DealerLevelDO dealerLevelDO = dealerLevelMapper.selectByPrimaryKey(id);
        DealerLevelVO dealerLevelVO = new DealerLevelVO();
        BeanUtils.copyProperties(dealerLevelDO,dealerLevelVO);
        return dealerLevelVO;
    }
}
